package org.example.bootvue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.bootvue.entity.Article;
import org.example.bootvue.entity.Result;
import org.example.bootvue.service.IArticleService;
import org.example.bootvue.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.net.http.HttpResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YourName
 * @since 2025-07-29
 */
@RestController
@RequestMapping("/article")
@ResponseBody
public class ArticleController {
    @Autowired
    IArticleService articleService;
@PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
}
@GetMapping
public Result<IPage> list(
        @RequestParam Integer pageNum,
        @RequestParam Integer pageSize,
        @RequestParam(required = false) Integer categoryId,
        @RequestParam(required = false)String state){
    LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(categoryId != null, Article::getCategoryId, categoryId)
            .eq(state != null, Article::getState, state);
    IPage iPage = new Page<>(pageNum, pageSize);
    IPage iPage1 = articleService.selectPage(iPage, wrapper);
    return Result.success(iPage1);
}
    @PutMapping
    public Result update(@RequestBody @Validated Article article){
    article.setUpdateTime(LocalDateTime.now());
    articleService.updateById(article);
    return Result.success();
    }
}
